package com.ly.user_system.utils;

import com.ly.user_system.common.ResultCode;
import lombok.Data;

import java.io.Serializable;

@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 消息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    // 成功静态方法
    public static <T> R<T> ok() {
        return restResult(null, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, ResultCode.SUCCESS.getCode(), msg);
    }

    // 失败静态方法
    public static <T> R<T> fail() {
        return restResult(null, ResultCode.FAILURE.getCode(), ResultCode.FAILURE.getMessage());
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, ResultCode.FAILURE.getCode(), msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, ResultCode.FAILURE.getCode(), ResultCode.FAILURE.getMessage());
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, ResultCode.FAILURE.getCode(), msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    // getters and setters...
}
